-- 单主的需求增加,这种针对性的写法太过繁琐,如果哪天这个代码别人接手了,我很抱歉
-- 当然如果你是mod作者,我也很抱歉,嘻嘻

local Upgrade = Class(function(self, inst)
    self.inst = inst
    -- 已经升级的碎屑类型："enlight" 或 "shadow"
    self.upgraded_type = nil

    if TheWorld.ismastersim then
        -- 确保有 planardamage 组件，初始基伤设为 0
        if not inst.components.planardamage and (not inst:HasTag("sw_pigweed_pouch"))
            and (not inst:HasTag("abigail_flower")) and (not inst:HasTag("abigail")) then
            inst:AddComponent("planardamage")
            inst.components.planardamage:SetBaseDamage(0)
        end
    end
end)

local function removeItem(item, num)
    if item.components.stackable then
        item.components.stackable:Get(num):Remove()
    else
        item:Remove()
    end
end

-- 判断能否进行升级
function Upgrade:CanUpgrade()
    if self.upgraded_type ~= nil and self.upgraded_type ~= "tent" then
        return false, "ALREADY_UPGRADED"
    end
    return true
end

-- 执行升级：shard_type 为 "enlight" 或 "shadow"
function Upgrade:DoUpgrade(shard_type)
    local ok, err = self:CanUpgrade()
    if not ok then
        return false, err
    end

    if self.inst:HasTag("sw_pigweed_pouch") then
        local goop = nil
        if shard_type == "enlight" then
            goop = SpawnPrefab("sw_enlight_pigweed_pouch")
        else
            goop = SpawnPrefab("sw_shadow_pigweed_pouch")
        end
        local x, y, z = self.inst.Transform:GetWorldPosition()
        goop.Transform:SetPosition(x, y, z)

        local owner = self.inst.components.inventoryitem ~= nil and self.inst.components.inventoryitem.owner or nil
        local holder = owner ~= nil and (owner.components.inventory or owner.components.container) or nil
        if holder ~= nil then
            removeItem(self.inst, 1)
            holder:GiveItem(goop)
        else
            removeItem(self.inst, 1)
            goop.components.inventoryitem:OnDropped(true, .5)
        end
        local fx = SpawnPrefab("chestupgrade_stacksize_fx")
        fx.Transform:SetPosition(goop.Transform:GetWorldPosition())
        return true
    elseif self.inst:HasTag("abigail_flower") or self.inst:HasTag("abigail") then
        -- 记录升级类型，防止重复升级或互斥
        self.upgraded_type = shard_type
        return true
    elseif self.inst.prefab == "siestahut" then
        if shard_type == "enlight" then
            self.inst.components.finiteuses:Repair(5)
            if self.upgraded_type == nil then
                self.inst.components.sleepingbag.health_tick = self.inst.components.sleepingbag.health_tick + 3
                self.inst.components.sleepingbag.hunger_tick = self.inst.components.sleepingbag.hunger_tick - 4 + (2 * TUNING.SLEEP_HUNGER_PER_TICK / 3)
                self.inst.components.sleepingbag.sanity_tick = self.inst.components.sleepingbag.sanity_tick + 4
            end

            self.upgraded_type = "tent"
            return true
        else
            return false, "MISMATCH"
        end
    elseif self.inst.prefab == "tent" then
        if shard_type == "enlight" then
            return false, "MISMATCH"
        else
            self.inst.components.finiteuses:Repair(5)
            if self.upgraded_type == nil then
                self.inst.components.sleepingbag.health_tick = self.inst.components.sleepingbag.health_tick + 3
                self.inst.components.sleepingbag.hunger_tick = self.inst.components.sleepingbag.hunger_tick - 4
                self.inst.components.sleepingbag.sanity_tick = self.inst.components.sleepingbag.sanity_tick + 4
            end

            self.upgraded_type = "tent"
            return true
        end
    else
        -- 提升基座位面伤害 +30
        local pd = self.inst.components.planardamage
        pd:SetBaseDamage(pd:GetBaseDamage() + 30)

        -- 确保有 damagetypebonus 组件
        if not self.inst.components.damagetypebonus then
            self.inst:AddComponent("damagetypebonus")
        end

        local bonus = self.inst.components.damagetypebonus
        if shard_type == "enlight" then
            -- 对暗影阵营伤害提升 10%
            bonus:AddBonus("shadow_aligned", self.inst, 1.1, "sw_upgrade")
        else -- "shadow"
            -- 对月亮阵营伤害提升 10%
            bonus:AddBonus("lunar_aligned", self.inst, 1.1, "sw_upgrade")
        end

        -- 记录升级类型，防止重复升级或互斥
        self.upgraded_type = shard_type
        local fx = SpawnPrefab("chestupgrade_stacksize_fx")
        fx.Transform:SetPosition(self.inst.Transform:GetWorldPosition())
        return true
    end
end

-- 保存时写入升级类型
function Upgrade:OnSave()
    if self.upgraded_type then
        return { upgraded = self.upgraded_type }
    end
end

-- 加载时重现效果
function Upgrade:OnLoad(data)
    if data and data.upgraded then
        -- 直接调用，不重复触发 CanUpgrade 检查
        self.upgraded_type = data.upgraded
        if (not self.inst:HasTag("abigail_flower")) and (not self.inst:HasTag("abigail")) and (not self.inst:HasTag("tent")) then
            -- 恢复基座位面伤害 +30
            local pd = self.inst.components.planardamage
            pd:SetBaseDamage(pd:GetBaseDamage() + 30)
            -- 恢复属性加成
            if not self.inst.components.damagetypebonus then
                self.inst:AddComponent("damagetypebonus")
            end
            local bonus = self.inst.components.damagetypebonus
            if data.upgraded == "enlight" then
                bonus:AddBonus("shadow_aligned", self.inst, 1.1, "sw_upgrade")
            elseif data.upgraded == "shadow" then
                bonus:AddBonus("lunar_aligned", self.inst, 1.1, "sw_upgrade")
            end
        elseif self.inst:HasTag("abigail") then
            -- 恢复基座位面伤害 +20
            local pd = self.inst.components.planardamage
            pd:SetBaseDamage(pd:GetBaseDamage() + 20)
            -- 恢复属性加成
            local bonus = self.inst.components.damagetypebonus
            if data.upgraded == "enlight" then
                bonus:AddBonus("shadow_aligned", self.inst, 1.1, "sw_upgrade")
            elseif data.upgraded == "shadow" then
                bonus:AddBonus("lunar_aligned", self.inst, 1.1, "sw_upgrade")
            end
        elseif self.inst:HasTag("tent") then
            if self.inst.prefab == "siestahut" then
                self.inst.components.sleepingbag.health_tick = self.inst.components.sleepingbag.health_tick + 3
                self.inst.components.sleepingbag.hunger_tick = self.inst.components.sleepingbag.hunger_tick - 4 + (2 * TUNING.SLEEP_HUNGER_PER_TICK / 3)
                self.inst.components.sleepingbag.sanity_tick = self.inst.components.sleepingbag.sanity_tick + 4
            elseif self.inst.prefab == "tent" then
                self.inst.components.sleepingbag.health_tick = self.inst.components.sleepingbag.health_tick + 3
                self.inst.components.sleepingbag.hunger_tick = self.inst.components.sleepingbag.hunger_tick - 4
                self.inst.components.sleepingbag.sanity_tick = self.inst.components.sleepingbag.sanity_tick + 4
            end
        end
    end
end

return Upgrade
